package com.example.springcloud.controller;

import com.example.springcloud.common.ResponseEntity;
import com.example.springcloud.common.entity.User;
import com.example.springcloud.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 莫磊
 * @date 2025/8/20
 * @description
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户接口", description = "提供用户的数据操作接口")
public class UserController {

    @Resource
    private UserService service;

    @GetMapping("/getById/{id}")
    @Operation(summary = "根据id查询用户", description = "返回指定id的用户", responses = {
            @ApiResponse(
                description = "成功获取到部门信息", responseCode = "200",
                    content = @Content(schema = @Schema(implementation = User.class))
            )
    }
    )
    public ResponseEntity<User> getById(@PathVariable("id") Long id) {
        User user =  service.getById(id);
        Assert.notNull(user, "用户不存在");
        return ResponseEntity.success(user);
    }

    @PostMapping("/addUser")
    @Operation(summary = "添加用户")
    public ResponseEntity<User> addUser(User user) {
        int i = service.addUser((com.example.springcloud.entity.User) user);
        Assert.isTrue(i > 0, "添加用户失败");
        return ResponseEntity.success(user);
    }

    @Operation(summary = "根据用户名称模糊查询用户",
            parameters = @Parameter(name = "name", description = "用户名称"))
    @GetMapping("/findByNameLike/{name}")
    public ResponseEntity<List<User>> findByNameLike(@PathVariable("name") String name) {
        return ResponseEntity.success((List)service.findByNameLike(name));
    }

}
